﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.PBKT;
using Entities.Admin;
using BLL.PBKT;

namespace WindowsForms.PBKT
{
	public partial class UCTrongTai : UserControl
	{
		public UCTrongTai()
		{
			InitializeComponent();

		}
		private void LoadDGVTrongTai(List<TrongTai> list)
		{
			dgvDSTrongTai.DataSource = list;
			int i = 0;

			for (i = 0; i < dgvDSTrongTai.RowCount; i++)
			{
				TrongTai tt = TrongTai.Objects.GetById(dgvDSTrongTai.Rows[i].Cells[0].Value);
				DiaChi dc = DiaChi.Objects.GetById(tt.MaDiaChi);
				dgvDSTrongTai.Rows[i].Cells[2].Value = dc.ToString();
			}


		}

		private void UCTrongTai_Load(object sender, EventArgs e)
		{
			dgvDSTrongTai.AutoGenerateColumns = false;
		}

		private void btnLoad_Click(object sender, EventArgs e)
		{
			LoadDGVTrongTai(TrongTai.Objects.All().ToList());
		}

		private void dgvDSTrongTai_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			try
			{
				if (e.ColumnIndex == 3)
				{
					TrongTai tt = TrongTai.Objects.GetById(dgvDSTrongTai.Rows[e.RowIndex].Cells[0].Value.ToString());
					if (Arbitration.Exit(tt))
					{
						if (TrongTai.Objects.Delete(tt))
						{
							MessageBox.Show("Xóa thành công");
							btnLoad.PerformClick();
						}
						else
							MessageBox.Show("Xóa không thành công");
					}
					else
						MessageBox.Show("Xóa không thành công!\n Bạn phải xóa trong lịch thi đấu");
				}
			
				if (e.ColumnIndex == 4)
				{
					TrongTai tt = TrongTai.Objects.GetById(dgvDSTrongTai.Rows[e.RowIndex].Cells[0].Value.ToString());
					FormTrongTaiThem frm = new FormTrongTaiThem(tt);
					frm.ShowDialog();
					btnLoad.PerformClick();
				}
			}
			catch (Exception ex)
			{

				MessageBox.Show(ex.Message);
			}

		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			FormTrongTaiThem frm = new FormTrongTaiThem(null);
			frm.ShowDialog();
			btnLoad.PerformClick();
		}

		private void btnFind_Click(object sender, EventArgs e)
		{
		
		}
	}
}
